# 错误码
import logging


def some_function():
    return -1


def foo():
    r = some_function()
    if r == (-1):
        return (-1)
    # do something
    return r


print(foo())

# try...except...finally...
try:
    print('try...')
    r = 10 / 0
    print('result:', r)
except ZeroDivisionError as e:
    print('except:', e)
finally:
    print('finally...')
print('END')


def foo(s):
    return 10 / int(s)


def bar(s):
    logging.warning(foo(s) * 2)
    return foo(s) * 2


def main(str):
    try:
        bar(str)
    except Exception as e:
        print('Error:', e)
    else:
        print('no error!')
    finally:
        print('finally...')


print(main('0'))
print('-----------------------------------------------')
print(main('1'))
print('************************************************************')


# 调用栈
# 如果错误没有被捕获，它就会一直往上抛，最后被Python解释器捕获，打印一个错误信息，然后程序退出。来看看err.py：

# err.py:
def foo2(s):
    return 10 / int(s)


def bar2(s):
    return foo(s) * 2


def main2():
    bar('0')


def test(str):
    logging.error(str)
    logging.debug(str)
    logging.warning(str)
    logging.info(str)

test('sgregregege')
# print(main2())
